<?php

/**
 * 实例化 model
 * powered by jeff 2011-5-20
 * @param unknown_type $model_name
 * @param unknown_type $params
 * @param unknown_type $is_new
 */
function api_model($model_name = null, $params = array(), $is_new = false)
{
    static $models = array();
    $model_hash = md5($model_name . 'api' . var_export($params, true));
    if ($is_new || !isset($models[$model_hash])) {
        $model_file = ROOT_PATH . '/includes/models/' . $model_name . '.model.php';
        if (is_file($model_file)) {
            include_once $model_file;
            $model_class_name = ucfirst($model_name) . 'Model';
        }

        $api_model_file = API_ROOT_PATH . '/models/' . $model_name . '.api.model.php';

        if (is_file($api_model_file)) {
            include_once $api_model_file;
            $model_class_name = ucfirst($model_name) . 'ApiModel';
        }

        if ($is_new) {
            return new $model_class_name($params, db());
        }
        $models[$model_hash] = new $model_class_name($params, db());
    }


    if (!empty($model_class_name)) {
        return $models[$model_hash];
    }
}